import * as bm from "/js/baseModule.js";

export class Module extends bm.baseModule {
    constructor(view) {
        super(view);

        //套餐详情渲染
        let that = this;
        let data = globalData;
        this.packInfo = new Vue({
            el: '#ValueAddService_vue',
            data: data,
            computed: {
                /**优惠活动是否到期*/
                expired: function () {
                    if (globalData.pack.id == 0) {
                        return true
                    }
                    let now = new Date().getTime();
                    let endDate = new Date(globalData.packList.discount_pack_set.deadline).getTime();
                    let dayTransMs = 1000 * 60 * 60 * 24;//上一行代码得出的结果是截止日期当天的0点0分，所以需要在此基础上加24小时；
                    return now > endDate + dayTransMs
                },
            },
            methods: {
                endTime: function (endTime) {
                    let data = "";
                    if (endTime == "") {
                        return data;
                    }
                    let time = endTime.toString().split('-');
                    if (time[1].substr(0, 1) == 0) {
                        time[1] = time[1].substr(1, 2);
                    }
                    if (time[2].substr(0, 1) == 0 && time[2].substr(1, 2) != 0) {
                        time[2] = time[2].substr(1, 2);
                    }
                    data = time[0] + "年" + time[1] + "月" + time[2] + "日";
                    return data;
                },
            }
        });
        //获取套餐价目列表
        api.getPackSet({
            succ: function (json) {
                $.extend(true, globalData.packList.price_pack_set, json.price_pack_set);
                $.extend(true, globalData.packList.dispatch_pack_set, json.dispatch_pack_set);
                $.extend(true, globalData.packList.sms_pack_set, json.sms_pack_set);
                $.extend(true, globalData.packList.discount_pack_set, json.discount_pack_set);
                $.extend(true, globalData.packList.bronze_medal_discount_set, json.bronze_medal_discount_set);
            }
        });
    }
    afterInstall() {
        let that = this;
        if (globalData.entryPage == 'pack/MyPack') {
            //获取我的套餐信息
            api.getMyPack({
                succ: function (data) {
                    $.extend(true, that.packInfo.pack, data.pack);
                    $.extend(true, globalData.pack, data.pack);
                    that.setData();
                    funs.removeWelcome();
                },
                fail: function (error) {
                    if (error.status == 401) {
                        funs.openModal('modules/modals/CommonModal.JS', function (mod) {
                            mod.setType(1);
                        })
                    } else {
                        funs.tip(error.msg, '', 2000);
                    }
                }
            });

            // 如果是支付成功后回调页面，通知支付成功
            if(location.href.indexOf('fromAliPay')>-1){
                funs.tip("支付成功","","succ");
                let h = window.location.href.split('?')[0];
                window.location.replace(h);
            }
        }
    }

    /** 购买套餐 */
    _evt_openWeChatPay(obj) {
        let id = $(obj).attr('id');
        let that = this;
        /**
         * 支付宝下单
         */
        this.find('.sendImgPending').show();
        let money;
        $.each(globalData.packList.price_pack_set,function (i, item) {
            if(item.id == id){
                money = item.price;
            }
        });
        api.alipayOrder({set_id:id},{
            succ:function (json) {
                funs.openModal('modules/modals/AlipayModal.js',function (mod) {
                    that.find('.sendImgPending').hide();
                    mod.setCtx({"money":money,"alipay_code":json.bill.id})
                })
            },
            fail:function (error) {
                funs.tip(error.msg,'','fail')
            }
        });
    }

    /**购买派单和新项目提醒*/
    _evt_payCharge(obj, hit) {
        let type = $(obj).attr('type');
        let index = parseInt($(obj).attr('index'));
        let id = parseInt($(obj).attr('id'));
        if (type == '2' && globalData.user.level == 10) {
            return;
        }
        if (type == '1') {
            let list = globalData.packList.sms_pack_set;
            funs.openModal('modules/modals/CommonModal.js', function (mod) {
                mod.setType(14, { id: id, cost: list[index].bid_point, month: list[index].name });
            })
        } else if (type == '2') {
            let list = globalData.packList.dispatch_pack_set;
            funs.openModal('modules/modals/CommonModal.js', function (mod) {
                mod.setType(13, { id: id, cost: list[index].bid_point, point: list[index].dispatch_point });
            })
        } else if (type == '3') {
            let list = globalData.packList.bronze_medal_discount_set;
            funs.openModal('modules/modals/CommonModal.js', function (mod) {
                mod.setType(18, { id: id, cost: list[index].bid_point, month: list[index].name });
            })
        }
    }

    /**动态修改数据值*/
    setData() {
        this.find(".count").text(globalData.pack.bid_point);
        // this.find(".dispatch-point").text(globalData.pack.dispatch_point);
        // if (globalData.pack.sms_end_date) {
        //     this.find(".sms-point").text("无限量");
        // } else if (globalData.pack.sms_point == 0) {
        //     this.find(".sms-point").text(0);
        // } else {
        //     this.find(".sms-point").text(globalData.pack.sms_point);
        // }
    }

    _evt_openGrabRecharge() {
        funs.openModal('modules/modals/RechargeModal.js', null)
    }
}